<template>
  <el-container home-container class="home-container">
    <!-- 头部区域开始 -->
    <el-header>
      <!-- logo区域 -->
      <div class="logo">
        <img src="../assets/image/logo.png" />
        <span>电商管理</span>
      </div>
      <!-- 右侧按钮 -->
      <el-dropdown class="right-menu">
        <span class="el-dropdown-link">
          {{ username }} 你好!!
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>全屏</el-dropdown-item>
          <!-- 第三方组件中绑定事件加native修饰符 -->
          <el-dropdown-item @click.native="QUIT">退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <!-- 头部区域结束 -->
    <!-- 身体区域 -->
    <el-container>
      <!-- 左侧菜单区域 -->
      <el-aside :width="iscollapse ? '64px' : '240px'">
        <!-- 切换按钮 -->
        <div class="toggle">
          <i class="el-icon-s-fold" v-if="!iscollapse" @click="iscollapse = !iscollapse"></i>
          <i class="el-icon-s-unfold" v-if="iscollapse" @click="iscollapse = !iscollapse"></i>
        </div>
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          router
          :collapse="iscollapse"
          :collapse-transition="false"
        >
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + val.path"
              v-for="val in item.children"
              :key="val.id"
            >{{ val.authName }}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧菜单区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 导入辅助函数
import { mapGetters, mapMutations } from "vuex";
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      userInfo: JSON.parse(sessionStorage.getItem("userInfo")),
      iscollapse: false,
    };
  },
  created() {
    this.getMenuList();
  },
  computed: {
    ...mapGetters(["username"]),
  },
  methods: {
    ...mapMutations(["QUIT"]),
    async getMenuList() {
      let { data: res } = await this.$axios.get("menus");
      if (res.meta.status !== 200) return this.$message.err(res.meta.msg);
      this.menuList = res.data;
      // console.log(this.menuList);
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100vh;
}
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: #373d41;
  .right-menu {
    display: flex;
    align-items: center;
    cursor: pointer;
    span {
      color: #eaedf1;
    }
  }
  .logo {
    display: flex;
    align-items: center;
    img {
      height: 50px;
      margin-right: 10px;
      vertical-align: center;
    }
    span {
      font-size: 30px;
      color: #eaedf1;
      font-family: "宋体";
    }
  }
}
.el-aside {
  background-color: #333744;
  .toggle {
    font-size: 30px;
    text-align: center;
    color: #fff;
    line-height: 40px;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
    .iconfont {
      font-size: 25px;
      margin-right: 10px;
      color: #eaedf1;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>